import { loginOut } from '@/services/user';
import { history } from 'umi';
import { message } from 'antd';

const Model = {
  namespace: 'login',
  state: {
    status: undefined,
  },
  effects: {
    *logout({ payload }, { call }) {
      // delete token in localstorage
      const res = yield call(loginOut, payload);
      if (res.code == 200) {
        message.success('登出成功');
        window.localStorage.removeItem('token');
        if (window.location.pathname !== '/user/login') {
          history.replace({
            pathname: '/user/login',
            // search: stringify({
            // 	redirect: window.location.href,
            // }),
          });
        }
      } else {
        window.localStorage.removeItem('token');
        message.error('登出失败');
      }
    },
  },
  reducers: {},
};

export default Model;
